package com.xwm93up.basic.enumtips;

/**
 * @Description: desc
 * @Author: xwm93up
 * @Since: 2021/11/27 11:57
 */
public class JudgeRole {
    public static String judge(String roleName) {
        // 使用if/else
        /*String result = "";
        if (roleName.equals("ROLE_ROOT_ADMIN")) {
            result = "ROLE_ROOT_ADMIN: " + "has AAA permission";
        } else if (roleName.equals("ROLE_ORDER_ADMIN")) {
            result = "ROLE_ORDER_ADMIN: " + "has BBB permission";
        } else if (roleName.equals("ROLE_NORMAL")) {
            result = "ROLE_NORMAL: " + "has CCC permission";
        } else {
            result = "XXX";
        }
        return result;*/

        // 用枚举一行代码搞定
        // return RoleEnum.valueOf(roleName).op();

        // 使用工厂模式
        // return  RoleFactory.getOp(roleName).op();

        // 使用枚举调用
        return RoleEnum.valueOf(roleName).op();
    }

    public String judge(RoleOperation roleOperation) {
        RoleContext roleContext = new RoleContext(roleOperation);
        return roleContext.execute();
    }

    public static void main(String[] args) {
        JudgeRole judgeRole = new JudgeRole();
        String result1 = judgeRole.judge(new RootAdminRole("ROLE_ROOT_ADMIN"));
        System.out.println(result1);
        String result2 = judgeRole.judge(new OrderAdminRole("ROLE_ORDER_ADMIN"));
        System.out.println(result2);
        String result3 = judgeRole.judge(new NormalRole("ROLE_NORMAL"));
        System.out.println(result3);
        //System.out.println(judge("ROLE_ROOT_ADMIN"));
    }
}
